package com.wzy.leecode.linked;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: wzy
 * @Date: 2021/11/18/20:45
 * @Description:
 */
public class ExerLinked {

    /**
     * 添加节点
     *
     * @param head 头节点
     * @param add  新添加的节点
     */
    public static void add(Node head, Node add) {
        if (head == null)
            return;
        while (head.next != null) {
            head = head.next;
        }
        head.next = add;
    }

    /**
     * 顺序打印
     *
     * @param head 头节点
     */
    public static void print(Node head) {
        if (head == null) {
            System.out.println("链表是空的");
        }
        while (head != null) {
            System.out.print(head.value + " ");
            head = head.next;
        }
    }

    /**
     * 逆序打印
     *
     * @param head
     */
    public static void reversePrinter(Node head) {
        if (head.next != null) {
            reversePrinter(head.next);
            System.out.print(head.value + " ");
        }
    }

    public static Node reverse(Node head) {
        if (head == null || head.next == null) {
            return head;
        }
        Node n1 = head;
        Node n2 = head.next;
        n1.next = null;
        while (n2.next != null) {
            Node n3 = n2.next;
            n2.next = n1;
            n1 = n2;
            n2 = n3;
        }
        n2.next = n1;
        return n2;
    }

    public static void main(String[] args) {
        Node node = new Node(1);
        for (int i = 2; i < 10; i++) {
            ExerLinked.add(node, new Node(i));
        }
        ExerLinked.print(node);
        System.out.println();
        Node reverse = reverse(node);
        ExerLinked.print(reverse);
        List<ListNode> list = new ArrayList<>();
        Collections.sort(list, new Comparator<ListNode>() {
            @Override
            public int compare(ListNode o1, ListNode o2) {
                return o1.val - o2.val;
            }
        });
    }


}
